package com.tools.scanner.ui.qrcode.scanner

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.mlkit.vision.barcode.common.Barcode
import com.tools.scanner.R
import com.tools.scanner.databinding.FragmentLinkScannerBinding
import com.tools.scanner.utils.copyText
import com.tools.scanner.utils.openBrowser
import com.tools.scanner.utils.showToast

class UrlFragment : BaseScannerFragment() {

    private lateinit var mBinding: FragmentLinkScannerBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentLinkScannerBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val urlBookmark = getData(Barcode.UrlBookmark::class.java) ?: return

        mBinding.tvContent.text = urlBookmark.url

        mBinding.tvCopy.setOnClickListener {
            urlBookmark.url?.copyText()
            R.string.copy_success.showToast()
        }

        mBinding.tvOpen.setOnClickListener {
            urlBookmark.url?.openBrowser()
        }
    }

}